/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: TradeDataManager.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.data_manager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import io.bhex.baselib.constant.Fields;
import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.HttpUtils;
import io.bhex.baselib.network.Utils.CodeUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.network.params.GetParams;
import io.bhex.baselib.network.params.PostParams;
import io.bhex.baselib.network.response.ResponseListener;
import io.bhex.sdk.Urls;
import io.bhex.sdk.account.UserInfo;
import io.bhex.sdk.socket.NetWorkObserver;
import io.bhex.sdk.socket.WebSocketBaseBean;
import io.bhex.sdk.trade.bean.AssetListResponse;
import io.bhex.sdk.trade.bean.AssetListSocketResponse;
import io.bhex.sdk.trade.bean.FuturesAssetListResponse;
import io.bhex.sdk.trade.bean.FuturesAssetListSocketResponse;
import io.bhex.sdk.trade.bean.FuturesHoldOrderSocketResponse;
import io.bhex.sdk.trade.bean.FuturesOrderSocketResponse;
import io.bhex.sdk.trade.bean.FuturesTradableInfoSocketResponse;
import io.bhex.sdk.trade.bean.OpenOrderResponse;
import io.bhex.sdk.trade.bean.OptionAssetListResponse;
import io.bhex.sdk.trade.bean.OptionAssetListSocketResponse;
import io.bhex.sdk.trade.bean.OptionHoldOrderResponse;
import io.bhex.sdk.trade.bean.OptionHoldOrderSocketResponse;
import io.bhex.sdk.trade.bean.OrderBean;
import io.bhex.sdk.trade.bean.OrderRequestSocketBean;
import io.bhex.sdk.trade.bean.OrderSocketResponse;
import io.bhex.sdk.trade.futures.bean.EntrustOrderResponse;
import io.bhex.sdk.trade.futures.bean.FuturesOrderResponse;
import io.bhex.sdk.trade.futures.bean.FuturesPositionOrder;
import io.bhex.sdk.trade.futures.bean.FuturesPositionOrderResponse;
import io.bhex.sdk.trade.futures.bean.TradableBean;
import io.bhex.sdk.trade.futures.bean.TradableBeanResponse;

public class TradeDataManager {
    private AssetListResponse mAssetListResponse = new AssetListResponse();
    LinkedHashMap<String,AssetListResponse.BalanceBean> tokenMap = new LinkedHashMap<String,AssetListResponse.BalanceBean>();
    private OptionAssetListResponse mOptionAssetListResponse = new OptionAssetListResponse();
    LinkedHashMap<String,OptionAssetListResponse.OptionAssetBean> optionTokenMap = new LinkedHashMap<String,OptionAssetListResponse.OptionAssetBean>();
    private FuturesAssetListResponse mFuturesAssetListResponse = new FuturesAssetListResponse();
    LinkedHashMap<String,FuturesAssetListResponse.FuturesAssetBean> futuresTokenMap = new LinkedHashMap<String,FuturesAssetListResponse.FuturesAssetBean>();
    LinkedHashMap<String, TradableBean> futuresTradableInfoMap = new LinkedHashMap<String,TradableBean>();


    private static TradeDataManager mInstance;
    public static TradeDataManager GetInstance() {
        if (mInstance == null) {

            mInstance = new TradeDataManager();
        }

        return mInstance;
    }


    TradeDataManager(){
        getRequestCurrentBalance(null);
        getRequestOptionCurrentBalance(null);
        getRequestFuturesCurrentBalance(null);
        RequestBalance(new NetWorkObserver<AssetListSocketResponse>(){

            @Override
            public void onShowUI(AssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<AssetListResponse>(){

            @Override
            public void onShowUI(AssetListResponse response) {
                if(response != null && response.getBalance() != null){
                    setAsset(response.getBalance());
                }
            }

            @Override
            public void onError(String response) {

            }
        });
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();
        HttpUtils.getInstance().request(params, AssetListResponse.class, new SimpleResponseListener<AssetListResponse>() {

            @Override
            public void onSuccess(AssetListResponse response) {
                super.onSuccess(response);
                if (response != null)
                    setAsset(response.getBalance());
            }
        });

        RequestOptionBalance(new NetWorkObserver<OptionAssetListSocketResponse>(){

            @Override
            public void onShowUI(OptionAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setOptionAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<OptionAssetListResponse>(){

            @Override
            public void onShowUI(OptionAssetListResponse response) {
                if(response != null && response.array != null){
                    setOptionAsset(response.array);
                }
            }

            @Override
            public void onError(String response) {

            }
        });
        PostParams optionParams = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_OPTION_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();
        HttpUtils.getInstance().request(optionParams, OptionAssetListResponse.class, new SimpleResponseListener<OptionAssetListResponse>() {

            @Override
            public void onSuccess(OptionAssetListResponse response) {
                super.onSuccess(response);
                if (response != null)
                    setOptionAsset(response.array);
            }
        });

        RequestFuturesBalance(new NetWorkObserver<FuturesAssetListSocketResponse>(){

            @Override
            public void onShowUI(FuturesAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setFuturesAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<FuturesAssetListResponse>(){

            @Override
            public void onShowUI(FuturesAssetListResponse response) {
                if(response != null && response.array != null){
                    setFuturesAsset(response.array);
                }
            }

            @Override
            public void onError(String response) {

            }
        });
        PostParams FuturesParams = BParamsBuilder.post()
                .url(Urls.API_FUTURES_ASSET_AVAILABLE)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();
        HttpUtils.getInstance().request(FuturesParams, FuturesAssetListResponse.class, new SimpleResponseListener<FuturesAssetListResponse>() {

            @Override
            public void onSuccess(FuturesAssetListResponse response) {
                super.onSuccess(response);
                if (response != null)
                    setFuturesAsset(response.array);
            }
        });
    }


    public AssetListResponse.BalanceBean getBalanceBean(String tokenId){
        return tokenMap.get(tokenId);
    }

    public OptionAssetListResponse.OptionAssetBean getOptionBalanceBean(String tokenId){
        return optionTokenMap.get(tokenId);
    }

    public FuturesAssetListResponse.FuturesAssetBean getFuturesBalanceBean(String tokenId){
        return futuresTokenMap.get(tokenId);
    }

    public void RequestOrder(NetWorkObserver<OrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "order";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }

    public void RequestOptionOrder(NetWorkObserver<OrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "option_order";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }

    public void RequestOptionHoldOrder(NetWorkObserver<OptionHoldOrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "option_position";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OptionHoldOrderSocketResponse.class, handler);
    }

    /** 币币订单 *******************************/

    /**
     * 币币-当前委托
     * @param symbolId
     * @param listener
     */
    public void RequestSymbolOrder(final String symbolId, final ResponseListener listener) {

        RequestOrder(new NetWorkObserver<OrderSocketResponse>(){

            @Override
            public void onShowUI(OrderSocketResponse response) {
                if(response != null && response.data != null){
                    List<OrderBean> list =  new ArrayList<>();
                    for (int i = 0; i < response.data.size(); i++) {
                        OrderBean orderBean = response.data.get(i);
                        if(orderBean != null && orderBean.getSymbolId().equalsIgnoreCase(symbolId)){
                            list.add(orderBean);
                        }
                    }
                    if(listener != null && list.size() > 0){
                        OpenOrderResponse openOrderResponse = new OpenOrderResponse();
                        openOrderResponse.setArray(list);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 币币-当前委托 SOCKET
     * @param listener
     */
    public void RequestOrder(final ResponseListener listener) {

        RequestOrder(new NetWorkObserver<OrderSocketResponse>(){

            @Override
            public void onShowUI(OrderSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null && response.data.size() > 0){
                        OpenOrderResponse openOrderResponse = new OpenOrderResponse();
                        openOrderResponse.setArray(response.data);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /** 期权订单 *******************************/

    /**
     * 期权- 当前委托
     * @param symbolId
     * @param listener
     */
    public void RequestOptionSymbolOrder(final String symbolId, final ResponseListener listener) {

        RequestOptionOrder(new NetWorkObserver<OrderSocketResponse>(){

            @Override
            public void onShowUI(OrderSocketResponse response) {
                if(response != null && response.data != null){
                    List<OrderBean> list =  new ArrayList<>();
                    for (int i = 0; i < response.data.size(); i++) {
                        OrderBean orderBean = response.data.get(i);
                        if(orderBean != null && orderBean.getSymbolId().equalsIgnoreCase(symbolId)){
                            list.add(orderBean);
                        }
                    }
                    if(listener != null && list.size() > 0){
                        OpenOrderResponse openOrderResponse = new OpenOrderResponse();
                        openOrderResponse.setArray(list);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期权-当前委托 SOCKET
     * @param listener
     */
    public  void RequestOptionOrder(final ResponseListener listener) {

        RequestOptionOrder(new NetWorkObserver<OrderSocketResponse>(){

            @Override
            public void onShowUI(OrderSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null && response.data.size() > 0){
                        OpenOrderResponse openOrderResponse = new OpenOrderResponse();
                        openOrderResponse.setArray(response.data);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期权-持仓 SOCKET
     * @param symbolId
     * @param listener
     */
    public void RequestOptionHoldOrder(final String symbolId, final ResponseListener listener) {

        RequestOptionHoldOrder(new NetWorkObserver<OptionHoldOrderSocketResponse>(){

            @Override
            public void onShowUI(OptionHoldOrderSocketResponse response) {
                if(response != null && response.data != null){
                    List<OptionHoldOrderResponse.OptionHoldOrderBean> list =  new ArrayList<>();
                    for (int i = 0; i < response.data.size(); i++) {
                        OptionHoldOrderResponse.OptionHoldOrderBean orderBean = response.data.get(i);
                        //if(orderBean != null && orderBean.symbolId.equalsIgnoreCase(symbolId)){
                            list.add(orderBean);
                        //}
                    }
                    if(listener != null /*&& list.size() > 0*/){
                        OptionHoldOrderResponse openOrderResponse = new OptionHoldOrderResponse();
                        openOrderResponse.array = list;
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期权-持仓 SOCKET
     * @param listener
     */
    public  void RequestOptionHoldOrder(final ResponseListener listener) {

        RequestOptionHoldOrder(new NetWorkObserver<OptionHoldOrderSocketResponse>(){

            @Override
            public void onShowUI(OptionHoldOrderSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null && response.data.size() > 0){
                        OptionHoldOrderResponse openOrderResponse = new OptionHoldOrderResponse();
                        openOrderResponse.array = response.data;
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /** 期货订单 *******************************/

    public void RequestFuturesOrder(NetWorkObserver<FuturesOrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "futures_order";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, FuturesOrderSocketResponse.class, handler);
    }

    public void RequestFuturesHoldOrder(NetWorkObserver<FuturesHoldOrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "futures_position";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, FuturesHoldOrderSocketResponse.class, handler);
    }

    /**
     * 期货- 当前委托
     * @param symbolId
     * @param listener
     */
    public void RequestFuturesSymbolOrder(final String symbolId, final ResponseListener listener) {

        RequestFuturesOrder(new NetWorkObserver<FuturesOrderSocketResponse>(){

            @Override
            public void onShowUI(FuturesOrderSocketResponse response) {
                if(response != null && response.data != null){
                    List<FuturesOrderResponse> list =  new ArrayList<>();
                    for (int i = 0; i < response.data.size(); i++) {
                        FuturesOrderResponse orderBean = response.data.get(i);
                        if(orderBean != null && orderBean.getSymbolId().equalsIgnoreCase(symbolId)){
                            list.add(orderBean);
                        }
                    }
                    if(listener != null && list.size() > 0){
                        EntrustOrderResponse openOrderResponse = new EntrustOrderResponse();
                        openOrderResponse.setArray(list);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期货-当前委托 SOCKET
     * @param listener
     */
    public  void RequestFuturesOrder(final ResponseListener listener) {

        RequestFuturesOrder(new NetWorkObserver<FuturesOrderSocketResponse>(){

            @Override
            public void onShowUI(FuturesOrderSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null && response.data.size() > 0){
                        EntrustOrderResponse openOrderResponse = new EntrustOrderResponse();
                        openOrderResponse.setArray(response.data);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期货-持仓 SOCKET
     * @param symbolId
     * @param listener
     */
    public void RequestFuturesHoldOrder(final String symbolId, final ResponseListener listener) {

        RequestFuturesHoldOrder(new NetWorkObserver<FuturesHoldOrderSocketResponse>(){

            @Override
            public void onShowUI(FuturesHoldOrderSocketResponse response) {
                if(response != null && response.data != null){
                    List<FuturesPositionOrder> list =  new ArrayList<>();
                    for (int i = 0; i < response.data.size(); i++) {
                        FuturesPositionOrder orderBean = response.data.get(i);
                        if(orderBean != null && orderBean.getSymbolId().equalsIgnoreCase(symbolId)){
                            list.add(orderBean);
                        }
                    }
                    if(listener != null /*&& list.size() > 0*/){
                        FuturesPositionOrderResponse positionOrderResponse = new FuturesPositionOrderResponse();
                        positionOrderResponse.setReqSymbolId(symbolId);
                        positionOrderResponse.setArray(list);
                        listener.onSuccess(positionOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    /**
     * 期货-持仓 SOCKET
     * @param listener
     */
    public  void RequestFuturesHoldOrder(final ResponseListener listener) {

        RequestFuturesHoldOrder(new NetWorkObserver<FuturesHoldOrderSocketResponse>(){

            @Override
            public void onShowUI(FuturesHoldOrderSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null && response.data.size() > 0){
                        FuturesPositionOrderResponse openOrderResponse = new FuturesPositionOrderResponse();
                        openOrderResponse.setArray(response.data);
                        listener.onSuccess(openOrderResponse);
                    }
                }
            }
            @Override
            public void onError(String response) {

            }
        });
    }

    public void RequestOrderFilled(NetWorkObserver<OrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "order_filled";
        socketBaseBean.event = "sub";


        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }

    public void RequestMatch(NetWorkObserver<OrderSocketResponse> handler){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "match";
        socketBaseBean.event = "sub";

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }


    public void RequestBalance(NetWorkObserver<AssetListSocketResponse> handler, NetWorkObserver<AssetListResponse> handlerHttp){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "balance";
        socketBaseBean.event = "sub";

        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, params, AssetListSocketResponse.class, handler,AssetListResponse.class,handlerHttp);
    }
    public void RequestOptionBalance(NetWorkObserver<OptionAssetListSocketResponse> handler, NetWorkObserver<OptionAssetListResponse> handlerHttp){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "option_balance";
        socketBaseBean.event = "sub";

        PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_OPTION_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, params, OptionAssetListSocketResponse.class, handler,OptionAssetListResponse.class,handlerHttp);
    }

    public void RequestFuturesBalance(NetWorkObserver<FuturesAssetListSocketResponse> handler, NetWorkObserver<FuturesAssetListResponse> handlerHttp){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "futures_balance";
        socketBaseBean.event = "sub";

        PostParams params = BParamsBuilder.post()
                .url(Urls.API_FUTURES_ASSET_AVAILABLE)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                .build();

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, params, FuturesAssetListSocketResponse.class, handler,FuturesAssetListResponse.class,handlerHttp);
    }

    public void RequestTokenBalance(final String tokenId, final ResponseListener listener) {

        if(listener != null ) {
            if(tokenMap.get(tokenId)!= null)
                listener.onSuccess(tokenMap.get(tokenId));
            else{

                PostParams params = BParamsBuilder.post()
                        .url(Urls.API_ACCOUNT_GET_LIST)
                        .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                        .build();
                HttpUtils.getInstance().request(params, AssetListResponse.class, new SimpleResponseListener<AssetListResponse>() {

                    @Override
                    public void onSuccess(AssetListResponse response) {
                        super.onSuccess(response);
                        if (response != null) {
                            setAsset(response.getBalance());
                            if (listener != null) {
                                listener.onSuccess(tokenMap.get(tokenId));
                            }
                        }
                    }
                });
            }
        }

        RequestBalance(new NetWorkObserver<AssetListSocketResponse>(){

            @Override
            public void onShowUI(AssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setAsset(response.data);
                    if(listener != null){
                        listener.onSuccess(tokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<AssetListResponse>(){

            @Override
            public void onShowUI(AssetListResponse response) {
                if(response != null && response.getBalance() != null){
                    setAsset(response.getBalance());
                    if(listener != null){
                        listener.onSuccess(tokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }

    public void RequestOptionTokenBalance(final String tokenId, final ResponseListener listener) {

        if(listener != null ) {
            if(optionTokenMap.get(tokenId)!= null)
                listener.onSuccess(optionTokenMap.get(tokenId));
            else{

                PostParams params = BParamsBuilder.post()
                        .url(Urls.API_ACCOUNT_GET_OPTION_LIST)
                        .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                        .build();
                HttpUtils.getInstance().request(params, OptionAssetListResponse.class, new SimpleResponseListener<OptionAssetListResponse>() {

                    @Override
                    public void onSuccess(OptionAssetListResponse response) {
                        super.onSuccess(response);
                        if (response != null) {
                            setOptionAsset(response.array);
                            if (listener != null) {
                                listener.onSuccess(optionTokenMap.get(tokenId));
                            }
                        }
                    }
                });
            }
        }

        RequestOptionBalance(new NetWorkObserver<OptionAssetListSocketResponse>(){

            @Override
            public void onShowUI(OptionAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setOptionAsset(response.data);
                    if(listener != null){
                        listener.onSuccess(optionTokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<OptionAssetListResponse>(){

            @Override
            public void onShowUI(OptionAssetListResponse response) {
                if(response != null && response.array != null){
                    setOptionAsset(response.array);
                    if(listener != null){
                        listener.onSuccess(optionTokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }

    public void RequestFuturesTokenBalance(final String tokenId, final ResponseListener listener) {

        if(listener != null ) {
            if(futuresTokenMap.get(tokenId)!= null)
                listener.onSuccess(futuresTokenMap.get(tokenId));
            else{

                PostParams params = BParamsBuilder.post()
                        .url(Urls.API_FUTURES_ASSET_AVAILABLE)
                        .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId())
                        .build();
                HttpUtils.getInstance().request(params, FuturesAssetListResponse.class, new SimpleResponseListener<FuturesAssetListResponse>() {

                    @Override
                    public void onSuccess(FuturesAssetListResponse response) {
                        super.onSuccess(response);
                        if (response != null) {
                            setFuturesAsset(response.array);
                            if (listener != null) {
                                listener.onSuccess(futuresTokenMap.get(tokenId));
                            }
                        }
                    }
                });
            }
        }

        RequestFuturesBalance(new NetWorkObserver<FuturesAssetListSocketResponse>(){

            @Override
            public void onShowUI(FuturesAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    setFuturesAsset(response.data);
                    if(listener != null){
                        listener.onSuccess(futuresTokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        },new NetWorkObserver<FuturesAssetListResponse>(){

            @Override
            public void onShowUI(FuturesAssetListResponse response) {
                if(response != null && response.array != null){
                    setFuturesAsset(response.array);
                    if(listener != null){
                        listener.onSuccess(futuresTokenMap.get(tokenId));
                    }
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }


    /**
     * 获取期货可交易信息 SOCKET
     * @param tokenId
     * @param listener
     */
    public void RequestFuturesTradableInfo(final String tokenId, final ResponseListener listener) {
        if (listener != null) {
            TradableBean oldTradableBean = futuresTradableInfoMap.get(tokenId);
            if (oldTradableBean != null) {
                listener.onSuccess(oldTradableBean);
            }else{
                GetParams.Builder builder = new BParamsBuilder().get();
                builder.url(Urls.API_FUTURES_ASSET_TRADABLE)
                        .addParam("token_ids", tokenId);
                GetParams params = builder.build();
                HttpUtils.getInstance().request(params, TradableBeanResponse.class, new SimpleResponseListener<TradableBeanResponse>(){
                    @Override
                    public void onSuccess(TradableBeanResponse response) {
                        super.onSuccess(response);
                        if (CodeUtils.isSuccess(response)) {
                            setFuturesTradableInfo(response.getArray());
                            listener.onSuccess(futuresTradableInfoMap.get(tokenId));
                        }
                    }
                });
            }

            RequestFuturesTradableInfo(tokenId, new NetWorkObserver<FuturesTradableInfoSocketResponse>() {

                @Override
                public void onShowUI(FuturesTradableInfoSocketResponse response) {
                    if (response != null && response.data != null) {
                        if (listener != null && response.data.size() > 0) {
                            setFuturesTradableInfo(response.data);

//                            TradableBeanResponse tradableBeanResponse = new TradableBeanResponse();
//                            tradableBeanResponse.setArray(response.data);
                            listener.onSuccess(futuresTradableInfoMap.get(tokenId));
                        }
                    }
                }

                @Override
                public void onError(String response) {

                }
            }, new NetWorkObserver<TradableBeanResponse>() {
                @Override
                public void onShowUI(TradableBeanResponse response) {
                    if (listener != null) {
                        setFuturesTradableInfo(response.getArray());
                        listener.onSuccess(futuresTradableInfoMap.get(tokenId));
                    }
                }

                @Override
                public void onError(String response) {

                }
            });
        }

    }

    /**
     * 获取期货可交易信息
     * @param tokenId
     * @param handler
     */
    public void RequestFuturesTradableInfo(String tokenId,NetWorkObserver<FuturesTradableInfoSocketResponse> handler,NetWorkObserver<TradableBeanResponse> handlerHttp){
        WebSocketBaseBean socketBaseBean = new WebSocketBaseBean();
        socketBaseBean.topic = "futures_tradeable";
        socketBaseBean.event = "sub";

        GetParams.Builder builder = new BParamsBuilder().get();
        builder.url(Urls.API_FUTURES_ASSET_TRADABLE)
                .addParam("token_ids", tokenId);
        GetParams params = builder.build();

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, params, FuturesTradableInfoSocketResponse.class, handler,TradableBeanResponse.class,handlerHttp);
    }



    public void RequestCurrentOrder(String symbolId, String fromId, NetWorkObserver<OrderSocketResponse> handler){
        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "order";
        socketBaseBean.event = "req";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "current_order";
        socketBaseBean.extData.symbolId = symbolId;
        socketBaseBean.extData.limit = "20";
        socketBaseBean.extData.fromId = fromId;

        socketBaseBean.key = socketBaseBean.topic + socketBaseBean.extData.dataType;

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }
    public void RequestHistoryOrder(String symbolId, String fromId, NetWorkObserver<OrderSocketResponse> handler){
        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "order";
        socketBaseBean.event = "req";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "history_order";
        socketBaseBean.extData.symbolId = symbolId;
        socketBaseBean.extData.limit = "20";
        socketBaseBean.extData.fromId = fromId;

        socketBaseBean.key = socketBaseBean.topic + socketBaseBean.extData.dataType;
        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }
    public void RequestHistoryMatch(String symbolId, String fromId, NetWorkObserver<OrderSocketResponse> handler){

        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "match";
        socketBaseBean.event = "req";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "history_match";
        socketBaseBean.extData.symbolId = symbolId;
        socketBaseBean.extData.limit = "20";
        socketBaseBean.extData.fromId = fromId;


        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OrderSocketResponse.class, handler);
    }


    public void RequestCurrentBalance(NetWorkObserver<AssetListSocketResponse> handler){
        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "balance";
        socketBaseBean.event = "req";
        socketBaseBean.needReSub = "true";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "current_balance";
        socketBaseBean.key = socketBaseBean.topic + socketBaseBean.extData.dataType;

       /* PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId()).build();*/

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, AssetListSocketResponse.class, handler);
    }

    public void RequestOptionCurrentBalance(NetWorkObserver<OptionAssetListSocketResponse> handler){
        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "option_balance";
        socketBaseBean.event = "req";
        socketBaseBean.needReSub = "true";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "option_current_balance";
        socketBaseBean.key = socketBaseBean.topic + socketBaseBean.extData.dataType;
        //"token_ids":"USDT"

       /* PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId()).build();*/

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, OptionAssetListSocketResponse.class, handler);
    }

    public void RequestFuturesCurrentBalance(NetWorkObserver<FuturesAssetListSocketResponse> handler){
        OrderRequestSocketBean socketBaseBean = new OrderRequestSocketBean();
        socketBaseBean.topic = "futures_balance";
        socketBaseBean.event = "req";
        socketBaseBean.needReSub = "true";
        socketBaseBean.extData.accountId = UserInfo.getDefaultAccountId();
        socketBaseBean.extData.dataType = "futures_current_balance";
        socketBaseBean.key = socketBaseBean.topic + socketBaseBean.extData.dataType;
        //"token_ids":"USDT"

       /* PostParams params = BParamsBuilder.post()
                .url(Urls.API_ACCOUNT_GET_LIST)
                .addParam(Fields.FIELD_ACCOUNT_ID, UserInfo.getDefaultAccountId()).build();*/

        NetWorkApiManager.getTradeInstance().SubRequestNetwork(socketBaseBean, null, FuturesAssetListSocketResponse.class, handler);
    }

    public void release(){
        tokenMap.clear();
        mInstance = null;
    }

    /**
     * 请求资产余额
     * @param listener
     */
    public void getRequestCurrentBalance(final ResponseListener listener) {

        //如果 有资产变化监听
        if (mAssetListResponse != null && listener != null) {
            listener.onSuccess(mAssetListResponse);
            return;
        }

        RequestCurrentBalance(new NetWorkObserver<AssetListSocketResponse>(){
            @Override
            public void onShowUI(AssetListSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null)
                        listener.onSuccess(response.data);
                    mAssetListResponse.setBalance(response.data);
                    setAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }

    public void getRequestOptionCurrentBalance(final ResponseListener listener) {

        if (mOptionAssetListResponse != null && listener != null) {
            listener.onSuccess(mOptionAssetListResponse);
            return;
        }

        RequestOptionCurrentBalance(new NetWorkObserver<OptionAssetListSocketResponse>(){
            @Override
            public void onShowUI(OptionAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null)
                        listener.onSuccess(response.data);
                    mOptionAssetListResponse.array = response.data;
                    setOptionAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }

    public void getRequestFuturesCurrentBalance(final ResponseListener listener) {

        if (mFuturesAssetListResponse != null && listener != null) {
            listener.onSuccess(mFuturesAssetListResponse);
            return;
        }

        RequestFuturesCurrentBalance(new NetWorkObserver<FuturesAssetListSocketResponse>(){
            @Override
            public void onShowUI(FuturesAssetListSocketResponse response) {
                if(response != null && response.data != null){
                    if(listener != null)
                        listener.onSuccess(response.data);
                    mFuturesAssetListResponse.array = response.data;
                    setFuturesAsset(response.data);
                }
            }

            @Override
            public void onError(String response) {

            }
        });
    }

    private void setAsset(List<AssetListResponse.BalanceBean> response) {
        if (response != null && response != null && !response.isEmpty()) {

            for (AssetListResponse.BalanceBean balanceBean : response) {
                String token = balanceBean.getTokenId();
                AssetListResponse.BalanceBean itemBean = tokenMap.get(token);
                if (itemBean != null) {
                    tokenMap.remove(token);
                }

                tokenMap.put(token, balanceBean);
            }

        }
    }

    private void setOptionAsset(List<OptionAssetListResponse.OptionAssetBean> response) {
        if (response != null && response != null && !response.isEmpty()) {

            for (OptionAssetListResponse.OptionAssetBean balanceBean : response) {
                String token = balanceBean.tokenId;
                OptionAssetListResponse.OptionAssetBean itemBean = optionTokenMap.get(token);
                if (itemBean != null) {
                    optionTokenMap.remove(token);
                }

                optionTokenMap.put(token, balanceBean);
            }

        }
    }

    private void setFuturesAsset(List<FuturesAssetListResponse.FuturesAssetBean> response) {
        if (response != null && response != null && !response.isEmpty()) {

            for (FuturesAssetListResponse.FuturesAssetBean balanceBean : response) {
                String token = balanceBean.tokenId;
                FuturesAssetListResponse.FuturesAssetBean itemBean = futuresTokenMap.get(token);
                if (itemBean != null) {
                    futuresTokenMap.remove(token);
                }

                futuresTokenMap.put(token, balanceBean);
            }

        }
    }

    /**
     * 保存期货可交易信息
     * @param response
     */
    private void setFuturesTradableInfo(List<TradableBean> response) {
        if (response != null && response != null && !response.isEmpty()) {

            for (TradableBean tradableBean : response) {
                String tokenId = tradableBean.getTokenId();
                TradableBean itemBean = futuresTradableInfoMap.get(tokenId);
                if (itemBean != null) {
                    futuresTradableInfoMap.remove(tokenId);
                }

                futuresTradableInfoMap.put(tokenId, tradableBean);
            }

        }
    }

    public AssetListResponse.BalanceBean getTokenAsset(String token){
        if(tokenMap != null){
            return tokenMap.get(token);
        }
        return null;
    }

    public FuturesAssetListResponse.FuturesAssetBean getFuturesAsset(String token){
        if(futuresTokenMap != null){
            return futuresTokenMap.get(token);
        }
        return null;
    }


}
